/**
 * 发表新帖子
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/19
 */
var self = pic = '';

var index = 1;

var files = [];

document.addEventListener('plusready' , ready ,  false);

function ready(){
	
	self = plus.webview.currentWebview(); 
	
	var uid = self.uid;
	
	//提交反馈意见
	$('.add_demand').on('click' , function(){ 
		var title = demand = contacts = phone = email = company = '';
		$('.field').each(function(index){
				switch (index){
					case 0:
						title = $(this).val(); 
						break;
					case 1:
						demand = $(this).val();
						break;
					case 2:
						contacts = $(this).val();
						break;
					case 3:
						phone = $(this).val();
						break;
					case 4:
						email = $(this).val();
						break;
					case 5:
						company = $(this).val();
						break;
				}
		})
		
		
		if(!title.trim()){
			
			toast('标题不能为空哦'); return;
			
		} 
		
		if(!demand.trim()){
			
			toast('需求不能为空哦');return;
			
		} 
		
		if(!contacts.trim()){
			
			toast('联系人不能为空哦');return;
			
		} 
		
		if(!p1.test(phone)){
			
			toast('手机号码格式有误');return;
			
		} 
		
		if(!p3.test(email)){
			
			toast('邮箱格式有误');return;
			
		} 
		
		if(!company.trim()){
			
			toast('工作单位不能为空哦');return;
			
		}   
		plus.nativeUI.showWaiting('发布中...');
		
		$.ajax({
			type:"post",
			url:apiRoot +'/Home/Demand/add_demand/',
			data : {
				uid : self.uid ,
				tid : 38,
				title : title ,
				demand : demand,
				contacts : contacts,
				phone :phone,
				email : email,
				company :company
			},
			success : function(data){
				plus.nativeUI.closeWaiting();
				
				if(data > 0){
					
					toast('发布成功');
					
					mui.fire(plus.webview.getWebviewById(self.page) , 'add_demand' ,{
	 					id : data  ,
	 					demand : demand,
	 					title : title
					});
					self.close();
				}
			},
			error : function(e){
				
				console.log(JSON.stringify(e));
				
				plus.nativeUI.closeWaiting();
				
				toast('连接超时,请检查网络');
				
			}
		});
	})
	
}
